﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.IO;

namespace Hishop.Plugins.Payment.ChinaUnionPay
{
    public class WP_QuickPayUtils
    {
        /// <summary>
        /// 生成发送银联报文页面
        /// </summary>
        /// <param name="valueVo"></param>
        /// <returns></returns>
        public string createPayHtml(String[] valueVo)
        {
            return null;
        }

        /// <summary>
        /// 直接跳转银行支付页面
        /// </summary>
        /// <param name="valueVo"></param>
        /// <param name="bank"></param>
        /// <returns></returns>
        public string createPayHtml(String[] valueVo, string plan)
        {

            SortedDictionary<string, string> map = new SortedDictionary<string, string>(StringComparer.Ordinal);
            for (int i = 0; i < WP_QucickPayConf.reqVo.Length; i++)
            {
                map.Add(WP_QucickPayConf.reqVo[i], valueVo[i]);
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("<script language=\"javascript\">window.onload=function(){document.pay_form.submit();}</script>");
            sb.Append("<form id=\"pay_form\" name=\"pay_form\" action=\"").Append(WP_QucickPayConf.gateWay).Append("\" method=\"post\">");
            foreach (KeyValuePair<string, string> enrty in map)
            {
               
                sb.Append("<input type=\"hidden\" name=\"" + enrty.Key + "\" id=\"" + enrty.Key + "\" value=\"" + enrty.Value + "\" />");
            
            }
         
            
            string chk = SignData.sign(WP_QucickPayConf.merCode, plan);
            if (chk == null || chk.Length != 256)
            {
                 //
            }
            sb.Append("<input type=\"hidden\" name=\"ChkValue\" id=\"ChkValue\" value=\"" + chk + "\" />");
            sb.Append("</form>");
            return sb.ToString();
        }

      

        private string joinMapValue(SortedDictionary<string, string> map, char connector)
        {
            StringBuilder b = new StringBuilder();
            foreach (KeyValuePair<string, string> entry in map)
            {
                b.Append(entry.Key);
                b.Append('=');
                if (entry.Value != null)
                {
                    b.Append(entry.Value);
                }
                b.Append(connector);
            }
            return b.ToString();
        }

        /// <summary>
        /// get the md5 hash of a string
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string md5(string str)
        {
            if (str == null)
            {
                return null;
            }
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            System.Text.Encoding e = System.Text.Encoding.GetEncoding(WP_QucickPayConf.charset);
            byte[] fromData = e.GetBytes(str);
            byte[] targetData = md5.ComputeHash(fromData);
            string byte2String = null;
            for (int i = 0; i < targetData.Length; i++)
            {
                string strHex = targetData[i].ToString("X2").ToLower();
                byte2String += strHex;
            }
            return byte2String;
        }

        public string[] getResArr(string str)
        {
            string ReservedKey = "cupReserved=";
            int ReservedStartIndex = str.IndexOf(ReservedKey);
            int LeftBigBraketIndex = str.IndexOf('{', ReservedStartIndex + ReservedKey.Length);
            int RightBigBraketIndex = str.IndexOf('}', LeftBigBraketIndex);
            string ReservedValue = str.Substring(LeftBigBraketIndex, RightBigBraketIndex - LeftBigBraketIndex + 1);
            str = str.Replace(ReservedValue, "");
            string[] resArr = str.Split('&');
            for (int i = 0; i < resArr.Length; i++)
            {
                if (String.Compare(resArr[i], "cupReserved=") == 0)
                {
                    resArr[i] += ReservedValue;
                }
            }
            return resArr;
        }

        /// <summary>
        /// 查询方法
        /// </summary>
        /// <param name="strURL"></param>
        /// <param name="req"></param>
        /// <returns></returns>
        public string doPostQueryCmd(string strURL, string req)
        {
            HttpWebRequest send = (HttpWebRequest)WebRequest.Create(strURL);
            System.Text.Encoding e = System.Text.Encoding.GetEncoding(WP_QucickPayConf.charset);
            byte[] requestBytes = e.GetBytes(req);
            send.Method = "POST";
            send.ContentType = "application/x-www-form-urlencoded";
            send.ContentLength = requestBytes.Length;
            Stream requestStream = send.GetRequestStream();
            requestStream.Write(requestBytes, 0, requestBytes.Length);
            requestStream.Flush();
            requestStream.Close();

            HttpWebResponse receive = (HttpWebResponse)send.GetResponse();
            StreamReader sr = new StreamReader(receive.GetResponseStream(), e);
            string backstr = sr.ReadToEnd();
            sr.Close();
            receive.Close();
            return backstr;
        }
    }
}
